#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
WaterSupply项目主入口文件
水库群供水调度优化模型运行脚本
"""

import sys
import os
from loguru import logger

# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)

# 导入项目模块
from optmodel import configure_logger
from optmodel.set_up import load_water_supply_model
from optmodel.load_data import process_para_data
from optmodel.model import creat_model
from optmodel.solver import get_solver, set_solver_parameters, solve_model
from optmodel.output_data import save_result

def main():
    """主函数"""
    try:
        # 配置日志
        configure_logger()
        logger.info("=== 启动水库群供水调度优化模型 ===")
        
        # 1. 加载配置和数据
        config_path = os.path.join(project_root, "config.json")
        logger.info(f"加载配置文件: {config_path}")
        para = load_water_supply_model(config_path)
        logger.info("配置和数据加载完成")
        
        # 2. 处理数据
        logger.info("开始处理数据...")
        param = process_para_data(para)
        logger.info("数据处理完成")
        
        # 3. 创建优化模型
        logger.info("开始创建优化模型...")
        model = creat_model(param)
        logger.info("优化模型创建完成")
        
        # 4. 求解模型
        logger.info("开始求解模型...")
        solved = solve_model(model)
        if solved:
            logger.info("模型求解成功！")
            save_result(model)
        
    except Exception as e:
        logger.error(f"运行过程中发生错误: {e}")
        import traceback
        logger.error(traceback.format_exc())

if __name__ == "__main__":
    main()